#村庄盖建筑页进行操作


import time
from selenium.webdriver.common.by import By



class NewBuildingOperator:
    def __init__(self, driver, building_urls, build_sequence):
        self.driver = driver
        self.building_urls = building_urls
        self.build_sequence = build_sequence

    def check_and_upgrade_buildings(self):
        try:
            for village_url in self.building_urls:
                self.driver.get(village_url)
                time.sleep(3)
                self._build_structures_in_village()
        except Exception as e:
            print(f"在建筑页面操作时发生错误：{e}")

    def _build_structures_in_village(self):
        try:
            for build_url, (id, gid) in self.build_sequence.items():
                self.driver.get(build_url)
                time.sleep(3)
                try:
                    button = self.driver.find_element(By.CSS_SELECTOR, "button.textButtonV1.green.build")
                    button.click()
                    print(f"成功点击建筑升级按钮，建筑ID={id}，分类ID={gid}")
                except Exception as e:
                    print(f"未找到或无法点击按钮，建筑ID={id}，分类ID={gid}：{e}")
        except Exception as e:
            print(f"在建筑序列操作时发生错误：{e}")